#include "animaltionwindow.h"

#include <QPainter>
#include <QTimer>

Animaltionwindow::Animaltionwindow(QWidget *parent)
    : QWidget{parent}
{

}

void Animaltionwindow::showBetScore(int bet)
{
    if(bet == 1)
    {
        m_image.load(":/images/images/score1.png");
    }
    else if(bet == 2)
    {
        m_image.load(":/images/images/score2.png");
    }
    else if(bet == 3)
    {
        m_image.load(":/images/images/score3.png");
    }
    update();
    QTimer::singleShot(2000,this,&Animaltionwindow::hide);


}

void Animaltionwindow::showSequence(Type type)
{
    m_x = 0;
    QString name = (type == Pair)?":/images/images/liandui.png":":/images/images/shunzi.png";
    m_image.load(name);
    update();
    QTimer::singleShot(2000,this,&Animaltionwindow::hide);
}

void Animaltionwindow::showJokerBomb()
{
    m_x = 0;
    m_index = 0;
    QTimer *timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        m_index++;
        if(m_index >8)
        {
            timer->stop();
            timer->deleteLater();
            m_index = 8;
            hide();
        }
        QString name = QString(":/images/images/joker_bomb_%1.png").arg(m_index);
        m_image.load(name);
        update();
    });
    timer->start(60);
}

void Animaltionwindow::showBomb()
{
    m_x = 0;
    m_index = 0;
    QTimer *timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        m_index++;
        if(m_index >12)
        {
            timer->stop();
            timer->deleteLater();
            m_index = 12;
            hide();
        }
        QString name = QString(":/images/images/bomb_%1.png").arg(m_index);
        m_image.load(name);
        update();
    });
    timer->start(60);
}

void Animaltionwindow::showPlane()
{
    m_x = width();
    m_image.load(":/images/images/plane_1.png");
    setFixedHeight(m_image.height());
    update();

    int step = width()/5;
    QTimer *timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        static int dist = 0;
        static int timers = 0;
        dist +=5;
        if(dist >= step)
        {
            dist = 0;
            timers++;
            QString name = QString(":/images/images/plane_%1.png").arg(timers % 5 + 1);
            m_image.load(name);
        }
        if(m_x <= -110)
        {
            timer->stop();
            timer->deleteLater();
            dist = timers =0;
            update();
        }
        m_x -=5;
        update();

    });
    timer->start(15);
}

void Animaltionwindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.drawPixmap(m_x,0,m_image.width(),m_image.height(),m_image);
}
